package Guillermo_Santillan_CMSC137_Project;

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.util.ArrayList;

public class ServerChat implements Runnable {

    private Socket socket;
    private JTextArea msgTA;
    private String name;
    private JTextField msgTF;
    private ArrayList clients;
    private JComboBox clientsCB;
    private List clientsList;
    ChatApp parent;
    int id;
    String inp = "", outMsg = "";
    int sendToID, ctr = 0;
    BufferedReader buffRead = null;
    BufferedWriter buffWrite = null;

    public ServerChat(Socket socket, JTextArea msgTA, JTextField msgTF, String name, int id, ChatApp parent) {
        this.socket = socket;
        this.msgTA = msgTA;
        this.msgTF = msgTF;
        this.name = name;

        this.id = id;
        this.parent = parent;

        try {
            buffRead = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            buffWrite = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            Thread th = new Thread(this);
            th.start();
        } catch (IOException e) {
            System.exit(-1);
        }
    }

    public void run() {
        while (!inp.equals("LOGOUT_EXIT")) {
            try {
                inp = buffRead.readLine();

                if (!inp.equals("LOGOUT_EXIT")) {
                    outMsg = inp;
                    if (inp.equals("33xx1")) {
                        ctr = 3;
                    } else if (inp.equals("xx551")) {
                        ctr = 11;
                    } else if (ctr == 3) {
                        sendToID = Integer.parseInt(inp);
                        ctr = 4;
                    } else if (ctr == 4) {
                        parent.relayMsg(inp, sendToID);
                    }
                } else {
                    parent.closeClientConnection(id, outMsg);
                    socket.close();
                }
            } catch (IOException e) {
            }
        }
    }

    public String getName() {
        return name;
    }

    public void setID(int id) {
        this.id = id;
    }

    public void sendMsg(String mess) {
        try {
            buffWrite.write(mess);
            buffWrite.newLine();
            buffWrite.flush();
        } catch (Exception m) {
        }
    }

    public void sendFromTo(String mss) {
        try {
            buffWrite.write(mss);
            buffWrite.newLine();
            buffWrite.flush();
        } catch (Exception m) {
        }
    }

    public void clientOnline(String nem) {
        try {
            buffWrite.write("698xx");
            buffWrite.newLine();
            buffWrite.flush();
            buffWrite.write(nem);
            buffWrite.newLine();
            buffWrite.flush();
            buffWrite.write("x9844");
            buffWrite.newLine();
            buffWrite.flush();
        } catch (Exception m) {
        }
    }

    public void clientOffline(int id) {
        try {
            buffWrite.write("16x28");
            buffWrite.newLine();
            buffWrite.flush();
            buffWrite.write("" + id);
            buffWrite.newLine();
            buffWrite.flush();
            buffWrite.write("1x428");
            buffWrite.newLine();
            buffWrite.flush();
        } catch (Exception m) {
        }
    }

    public void sendClose() {
        try {
            buffWrite.write("Server_Exit_Connection_End");
            buffWrite.newLine();
            buffWrite.flush();
        } catch (Exception m) {
        }
    }
}